package com.wantong.jianpu.viewmodels;

import android.app.Application;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.wantong.jianpu.activitys.FaultDetailActivity;
import com.wantong.jianpu.adapters.MessageAdapter;
import com.wantong.jianpu.base.BaseResultBean;
import com.wantong.jianpu.beans.DeviceListBean;
import com.wantong.jianpu.beans.MessageBean;
import com.wantong.jianpu.event.MessageEvent;
import com.wantong.jianpu.models.OtherModel;
import com.wantong.jianpu.net.BaseObserver;
import com.wantong.jianpu.utils.Constants;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;

/**
 * @author PACY
 * @date 2022/4/24
 * description:
 */
public class MessageViewModel extends BaseViewModel {
    public MessageAdapter mAdapter;
    private int pageSize=20;
    private int pageNum=1;
    public SingleLiveEvent<Boolean> refreshEvent=new SingleLiveEvent<>();
    public OtherModel model;
    private String isReceived="0";
    public MessageViewModel(@NonNull Application application) {
        super(application);
        model=new OtherModel();
    }

    @Override
    protected void init() {
        super.init();
        initAdapter();
        loadData(true);
    }

    private void initAdapter(){
        mAdapter=new MessageAdapter();
        mAdapter.getLoadMoreModule().setOnLoadMoreListener(() -> {
            loadData(false);
        });
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Bundle bundle =new Bundle();
            bundle.putString("id",mAdapter.getItem(position).getBusinessId());
            startActivity(FaultDetailActivity.class,bundle);
            setMsgReceive(position);
        });
    }

    /**
     * 切换查询类型
     * @param received 0未读 3全部
     */
    public void setTypeRequest(String received){
        isReceived=received;
        loadData(true);
    }


    /**
     * 查询设备巡检列表
     */
    public void loadData(boolean isRefresh){
        if(isRefresh){
            pageNum=1;
            refreshEvent.setValue(true);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        if(isReceived.equals("0")){
            map.put("isReceived",isReceived);
        }
        model.messageList(map, new BaseObserver<MessageBean>() {
            @Override
            public void onSuccess(BaseResultBean<MessageBean> t) {
                if(isRefresh){
                    if(t.getData().getRows().isEmpty()){
                        showEmpty();
                    }else{
                        showContent();
                        mAdapter.setNewInstance(t.getData().getRows());
                    }
                }else{
                    mAdapter.addData(t.getData().getRows());
                    mAdapter.getLoadMoreModule().loadMoreComplete();
                    if(t.getData().getRows().size()<pageSize){
                        mAdapter.getLoadMoreModule().loadMoreEnd();
                    }
                }
                pageNum++;
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mAdapter.getLoadMoreModule().loadMoreFail();
            }

            @Override
            public void onComplete() {
                super.onComplete();
                refreshEvent.setValue(false);
            }
        },getLifecycleProvider());
    }

    /**
     * 设置消息已读
     */
    public void setMsgReceive(int position){
        mAdapter.getItem(position).setIsReceived("3");
        mAdapter.notifyItemChanged(position);
        Map<String,Object> map=new HashMap<>();
        map.put("id",mAdapter.getItem(position).getId());
        model.receive(map, new BaseObserver() {
            @Override
            public void onSuccess(BaseResultBean t) {
                EventBus.getDefault().post(new MessageEvent());
            }


        },getLifecycleProvider());
    }
}
